package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.entity.Student;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * 消息发送者/生产者
 */
@RestController
@RequiredArgsConstructor
public class ProducerController {

    private final RabbitTemplate rabbitTemplate;

    @Value("${shop.exchange}")
    private String exchange;

    @Value("${shop.routingKey}")
    private String routingKey;

    @RequestMapping("/send")
    public String send(){
        // Student stu = new Student(1001, "alice", 20, "男", 180.3);
        // rabbitTemplate.convertAndSend(exchange, routingKey, stu);

        // rabbitTemplate.convertAndSend("exchange2.fanout", "", "hello");

        // 为消息添加唯一标识id
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("exchange1.direct", "key1", "hello", correlationData);

        return "success";
    }

}
